package com.qf.controller;

import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/dashboard")
public class DtsAdminController {

    @Autowired
    DtsGoodsService goodsService;

    @Autowired
    DtsUserService userService;

    @Autowired
    DtsGoodsProductService goodsProductService;

    @Autowired
    DtsOrderService orderService;

    @Autowired
    DtsCategoryService categoryService;



    @RequestMapping("/chart")
    public Object chart(){
        Map<String,Object> data = new HashMap<>();
        //categorySell ： 分类和分类对应的销售额
        List<CategorySellAmts> categoryShellData = categoryService.findCategorySellData();


        //获取 大分类 名称， 存储到数组中
        String[] categoryNames = new String[categoryShellData.size()];
        for (int i = 0; i < categoryShellData.size(); i++) {
            CategorySellAmts categorySellAmts = categoryShellData.get(i);
            String name = categorySellAmts.getName();

            categoryNames[i] = name;
        }
        //组合数据
        CategorySellVo categorySellVo = new CategorySellVo();
        categorySellVo.setCategoryNames(categoryNames);
        categorySellVo.setCategorySellData(categoryShellData);

        //查询最近一个月 每天的订单总额，订单笔数
        List<DayStatis> orderStatis = orderService.getOrderAmts();


        //提取日期数据
        List<String> dayDataList  = fetchDateData(orderStatis);
        //转换为 数组格式
        String[] dayData = dayDataList.toArray(new String[]{});

        OrderAmtsVo orderAmtsVo = new OrderAmtsVo();
        //日期 列表
        orderAmtsVo.setDayData(dayData);

        //提取订单总额
        List<BigDecimal> orderAmtsList = orderStatis.stream().map(orderStati -> {
            return orderStati.getAmts();
        }).collect(Collectors.toList());

        //订单总额
        orderAmtsVo.setOrderAmtData(orderAmtsList.toArray(new BigDecimal[]{}));

        //提取订单总额
        List<Integer> orderCntsList = orderStatis.stream().map(orderStati -> {
            return orderStati.getCnts();
        }).collect(Collectors.toList());
        Integer[] orderCnts
                = orderCntsList.toArray(new Integer[]{});
        //订单数量
        orderAmtsVo.setOrderCntData(orderCnts);




        //统计一个月内 用户和订单 每天的数量
        //查询一个月内，每天的用户新增数量
        List<DayStatis> userDayStatis = userService.findUserStatis();

        UserOrderCntVo userOrderCntVo = new UserOrderCntVo();
        userOrderCntVo.setDayData(dayData);
        userOrderCntVo.setOrderCnt(orderCnts);

        List<Integer> userCnts = userDayStatis.stream().map(dayStati -> {
            return dayStati.getCnts();
        }).collect(Collectors.toList());

        userOrderCntVo.setUserCnt(userCnts.toArray(new Integer[]{}));

        data.put("categorySell", categorySellVo);
        data.put("orderAmts", orderAmtsVo );
        data.put("userOrderCnt", userOrderCntVo );

        return ResponseUtil.ok(data);
    }

    private List<String> fetchDateData(List<DayStatis> orderStatis) {
        return  orderStatis.stream().map(orderStati->{
            return orderStati.getDayStr();
        }).collect(Collectors.toList());
    }


    @RequestMapping("")
    public Object init(){
        Map<String,Object> data = new HashMap<>();
        //商品总数
        Integer goodsTotal = goodsService.getTotal();
        data.put("goodsTotal", goodsTotal);
        Integer userTotal = userService.getTotal();
        //用户的总数
        data.put("userTotal", userTotal);
        Integer productTotal = goodsProductService.getTotal();
        //货品的总数
        data.put("productTotal",productTotal );
        Integer orderTotal = orderService.getTotal();
        //订单总数
        data.put("orderTotal", orderTotal);
        return ResponseUtil.ok(data);
    }
}
